const {dialog, ipcMain} = require('electron');
let fs = require('fs');

ipcMain.handle('on-updatejsonfile-event', (e, filename,data) => {
    const overview = fs.readFileSync("./public/overview.json", 'utf8')
    let overview_json = JSON.parse(overview)
    for (let i = 0; i < overview_json.dbs.length; i++) {
        if (overview_json.dbs[i].file_name == filename){
            overview_json.dbs[i].database = data.database
            overview_json.dbs[i].name = data.name
            overview_json.dbs[i].exam_date = data.exam_date
            overview_json.dbs[i].duration = data.duration
            overview_json.dbs[i].question_volume = data.question_volume
            overview_json.dbs[i].file_name = filename
        }
    }
    fs.writeFileSync('./public/overview.json', JSON.stringify(overview_json), 'utf8', (err) =>{
        if (err) {
            return console.log(err)
        }
    });
    fs.writeFileSync('./public/'+filename,JSON.stringify(data), 'utf8', (err) =>{
        if (err) {
         return console.log(err)
        }
    });
})